#ifndef IPROPERTY_H
#define IPROPERTY_H
#pragma pack(push,1)

#include "../assetfilemgr.h"
#include "../formats/propertyformat.h"

class IProperty : public SPropertyData
{
public:
	static void	*AssetInit( IAssetFile *pFile, const CMasterIndexEntry *pEntry, void *context );
	static void	AssetUninit( void *ptr, void *context );
	static void	AssetFixup( CAssetFileMgr *pMgr, const IAssetFile *pFile, void *ptr, const CMasterIndexEntry *pEntry );

	static void	Register( CAssetFileMgr *pMgr )
	{
		pMgr->AddAssetType("PROPERTY",AssetInit,AssetUninit,AssetFixup);
	}

public:

	const bool	GetCharValue( const char *key, const char **pValue, const char *defaultValue=0 ) const
	{
		const IProperty *pp;
		for( pp=this;pp;pp=(const IProperty*)pp->parent )
		{
			*pValue = (const char*)pp->charValues.FindByHash(MakeStringHash(key));
			if( pValue[0]!=0 )
				return true;
		}
		*pValue = defaultValue;
		return false;
	}
	const bool	GetIntValue( const char *key, s32 *pValue, s32 defaultValue=0 ) const
	{
		u32				idx;
		const IProperty *pp;
		for( pp=this;pp;pp=(const IProperty*)pp->parent )
		{
			if( pp->intValues.ItemExists(MakeStringHash(key),&idx) )
			{
#pragma warning( disable : 4311 )
				*pValue = (s32)(int)pp->intValues.FindByIndex(idx);
#pragma warning( default : 4311 )
				return true;
			}
		}
		*pValue = defaultValue;
		return false;
	}
	const bool	GetFloatValue( const char *key, f32 *pValue, f32 defaultValue=0 ) const
	{
		u32				idx;
		const IProperty *pp;
		for( pp=this;pp;pp=(const IProperty*)pp->parent )
		{
			if( pp->floatValues.ItemExists(MakeStringHash(key),&idx) )
			{
#pragma warning( disable : 4311 )
				*(int*)pValue = (int)pp->floatValues.FindByIndex(idx);
#pragma warning( default : 4311 )
				return true;
			}
		}
		*pValue = defaultValue;
		return false;
	}
	const bool	GetVectorValue( const char *key, CVector *pValue, f32 defaultValueX=0.f,
																  f32 defaultValueY=0.f,
																  f32 defaultValueZ=0.f ) const
	{
		u32				idx;
		const IProperty *pp;
		for( pp=this;pp;pp=(const IProperty*)pp->parent )
		{
			bool bRet = false;
#pragma warning( disable : 4311 )
			if( pp->vectorValues.ItemExists(MakeStringHash(key),&idx) )
			{
				bRet = true;
				*(int*)&pValue->f[0] = (int)pp->vectorValues.FindByIndex(idx);
			}
			else
				pValue->f[0] = defaultValueX;

			if( pp->vectorValues.ItemExists(MakeStringHash(key)+1,&idx) )
				*(int*)&pValue->f[1] = (int)pp->vectorValues.FindByIndex(idx);
			else
				pValue->f[1] = defaultValueY;

			if( pp->vectorValues.ItemExists(MakeStringHash(key)+2,&idx) )
				*(int*)&pValue->f[2] = (int)pp->vectorValues.FindByIndex(idx);
			else
				pValue->f[2] = defaultValueZ;
#pragma warning( default : 4311 )

			if( bRet )
				return true;
		}
		return false;
	}

	const bool	GetVectorValue4( const char *key, CVector *pValue, f32 defaultValueX=0.f,
																  f32 defaultValueY=0.f,
																  f32 defaultValueZ=0.f,
																  f32 defaultValueW=0.f ) const
	{
		u32				idx;
		const IProperty *pp;
		for( pp=this;pp;pp=(const IProperty*)pp->parent )
		{
			bool bRet = false;
#pragma warning( disable : 4311 )
			if( pp->vectorValues.ItemExists(MakeStringHash(key),&idx) )
			{
				bRet = true;
				*(int*)&pValue->f[0] = (int)pp->vectorValues.FindByIndex(idx);
			}
			else
				pValue->f[0] = defaultValueX;

			if( pp->vectorValues.ItemExists(MakeStringHash(key)+1,&idx) )
				*(int*)&pValue->f[1] = (int)pp->vectorValues.FindByIndex(idx);
			else
				pValue->f[1] = defaultValueY;

			if( pp->vectorValues.ItemExists(MakeStringHash(key)+2,&idx) )
				*(int*)&pValue->f[2] = (int)pp->vectorValues.FindByIndex(idx);
			else
				pValue->f[2] = defaultValueZ;

			if( pp->vectorValues.ItemExists(MakeStringHash(key)+3,&idx) )
				*(int*)&pValue->f[3] = (int)pp->vectorValues.FindByIndex(idx);
			else
				pValue->f[3] = defaultValueW;
#pragma warning( default : 4311 )

			if( bRet )
				return true;
		}
		return false;
	}
};

#pragma pack(pop)
#endif
